package com.example.gramediamajalahmobile.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.gramediamajalahmobile.ArticleBean;

public class DBHelperFavorite {
	private static final String DB_NAME = "artikels_db";
	private static final int DB_VER = 1;
	public static final String TABLE_NAME = "favorite";
	public static final String COL_ID = "_id";
	public static final String COL_TITLE = "title";
	public static final String COL_SOURCE = "source";
	public static final String COL_VIEW = "view";
	public static final String COL_GAMBAR = "gambar";
	public static final String COL_BODY = "body";
	public static final String COL_TANGGAL = "tanggal";


	private static final String TAG = "articleDBAdapter";
	private DatabaseHelper dbHelper;
	private SQLiteDatabase db;

	private static final String DB_CREATE = "create table favorite (_id integer primary key ,title text not null,gambar text not null,tanggal text not null, view text not null,source text not null , body text not null);";

	private final Context context;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context) {
			// TODO Auto-generated constructor stub
			super(context, DB_NAME, null, DB_VER);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DB_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			Log.d(TAG, "upgrade DB");
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);

		}

	}

	public DBHelperFavorite(Context context) {
		this.context = context;
		// TODO Auto-generated constructor stub
	}

	public DBHelperFavorite open() throws SQLException {
		dbHelper = new DatabaseHelper(context);
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		dbHelper.close();
	}

	public void createFavorite(ArticleBean article) {
		ContentValues val = new ContentValues();
		val.put(COL_TITLE, article.getTitle());
		val.put(COL_GAMBAR, article.getGambar());
		val.put(COL_TANGGAL, article.getTanggal());
		val.put(COL_VIEW, article.getView());
		val.put(COL_SOURCE, article.getSource());
		val.put(COL_BODY, article.getBody());
		db.insert(TABLE_NAME, null, val);
	}

	public boolean deleteFavorite(int id) {
		return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0;
	}

	public Cursor getAllArticle() {
		return db.query(TABLE_NAME, new String[] {COL_ID, COL_TITLE,COL_GAMBAR,COL_TANGGAL, COL_VIEW, COL_SOURCE, COL_BODY }, null,
				null, null, null, null);
	}

	 public Cursor getSingleArticle(int id)
	 {
	 Cursor cursor = db.query(TABLE_NAME, new String[]
	 {
	 COL_ID, COL_TITLE,COL_GAMBAR,COL_TANGGAL, COL_VIEW, COL_SOURCE, COL_BODY
	 }, COL_ID + "=" + id, null, null, null, null);
	
	 if (cursor != null)
	 cursor.moveToFirst();
	
	 return cursor;
	 }

	public boolean updateFavorite(ArticleBean article) {
		ContentValues val = new ContentValues();
		val.put(COL_TITLE, article.getTitle());
		val.put(COL_GAMBAR, article.getTitle());
		val.put(COL_TANGGAL, article.getTitle());
		val.put(COL_VIEW, article.getTitle());
		val.put(COL_SOURCE, article.getTitle());
		val.put(COL_BODY, article.getTitle());
		return db.update(TABLE_NAME, val,
				COL_ID + "=" + article.getId_article(), null) > 0;
	}

}

// ContentValues values = new ContentValues();
// values.put("id", "s_id");
// values.put("title", "s_title");
// values.put("source", "s_source");
// values.put("view", "s_view");
// values.put("tanggal", "s_tanggal");
// values.put("gambar", "s_gambar");
// values.put("body", "s_body");
// db.insert(DATABASE_NAME, null, values);
